import { logout } from '@/api/login'
import { getRoles, removeAuth } from '@/utils/auth'

const user = {
  state: {
    roles: getRoles()
  },
  mutations: {
    SET_ROLES: (state, roles) => {
      state.roles = [roles]
    }
  },
  actions: {
    LogOut ({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout().then(() => {
          commit('SET_ROLES', [])
          removeAuth()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
